import { defineMock } from '@alova/mock';

const getData = (data) => {
  return {
    code: '0000',
    data,
    msg: 'ok'
  }
}

const allList = Array.from({ length: 3 })
  .map((_) => [
    {
      title: "August",
      cls: "class 1"
    },
    {
      title: "Marshall",
      cls: "class 3"
    },
    {
      title: "Maxwell",
      cls: "class 1"
    },
    {
      title: "Kevin",
      cls: "class 1"
    },
    {
      title: "Julian",
      cls: "class 2"
    },
    {
      title: "Maxwell",
      cls: "class 2"
    },
    {
      title: "August",
      cls: "class 1"
    },
    {
      title: "Maxwell",
      cls: "class 3"
    },
    {
      title: "Marshall",
      cls: "class 1"
    },
    {
      title: "William",
      cls: "class 1"
    }
  ])
  .flat()
  .map((item, i) => ({
    id: i + 1,
    ...item
  }));

const getArr = (query) => {
  let { page = 1, pageSize = 10, studentName, clsName } = query;
  page = Number(page);
  pageSize = Number(pageSize);
  const start = (page - 1) * pageSize;
  return allList
    .slice()
    .reverse()
    .filter(
      ({ title, cls }) =>
        (studentName
          ? title
              .toLocaleLowerCase()
              .indexOf(studentName.toLocaleLowerCase()) >= 0
          : true) && (clsName ? clsName === cls : true)
    )
    .slice(start, start + pageSize);
}

const geErrData = (data) => {
  return {
    code: '0100',
    data,
    msg: 'err'
  }
}
  const getAuth = () => {
    return {
      token: "7c613b13-dac8-4c50-a1f9-a46b8288a981"
    }
  }

  export default defineMock(
    {
      '[POST]/v1/auth': () => { return getData(getAuth())},
      '[POST]/getList': ({ data }) => {
        // ...
        const total = 21
        let list: any = []
        if (data.page === 3) {
          list = [
            {
              title: 'laset',
              id: '21'
            }
          ]
        } else {
          list = getArr(data)
        }
        return getData({
          total,
          list
        })
      }
    }
  );
